package pikasial.graphique;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.JWindow;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

public class SplashScreen extends JWindow {

	private static final long serialVersionUID = -4000555571958194612L;
	private JPanel panel;
	private JLabel label;
	private static final String SPLASH_PATH = "/img/PikasialLogo.png";

	public SplashScreen() {
		super(pikasial.Pikasial.getFrame());
		panel = new JPanel();
        URL url = getClass().getResource(SPLASH_PATH);
        ImageIcon ico = new ImageIcon(url);
        label = new JLabel(ico);
		panel.add(label);
		getContentPane().add(panel);
		pack();

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = label.getPreferredSize();
        setLocation(screenSize.width / 2 - (labelSize.width / 2),
                screenSize.height / 2 - (labelSize.height / 2));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                setVisible(false);
                dispose();
            }
        });
	}

	public void afficherSplashScreen() {
		setVisible(true);
		toFront();
		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(3000);
					SwingUtilities.invokeAndWait(new Runnable() {
						public void run() {
							setVisible(false);
							dispose();
						}
					});
				} catch (Exception ie) {
					ie.printStackTrace();
				}
			}
		}).start();
	}
}
